<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-10-19
 * Time: 13:34
 */
namespace app\index\controller;

use think\Db;
use think\Request;

class Staffsaves extends Base
{
    //原form数据
    /*public function index()
    {//获取员工信息
        $this->baseindex();
        if (isset($_GET['id'])) {
            $arr = input('get.id');
            $result = Db::table('oa_user u')->join('oa_user_role r', 'u.id=r.user_id', 'left')->where('u.id=' . $arr)->find();//获取员工信息
            $role = Db::table('oa_role')->select();
            $branch = Db::table('oa_user u')->field('b.id,b.branch')->join('oa_branch b', 'u.branch_id=b.id', 'left')->where('u.id=' . $arr)->find();//获取员工信息
            $branchs = Db::table('oa_branch')->field('id,branch')->where('region_id=' . $result['region_id'])->select();//获取部门信息
            $region = Db::table('oa_region')->select();
            //dump($branchs);die;
            $this->assign('region', $region);
            $this->assign('role', $role);
            $this->assign('branch', $branch);
            $this->assign('branchs', $branchs);
            $this->assign('userlist', $result);
            return $this->fetch();
        }
    }*/

    //新插件form数据
    public function index()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $id = input('get.id');
            //员工的个人详情
            $res = Db::table('oa_user u')->field('u.id,u.ch_name,u.en_name,u.identity_card,
            u.tel,u.sex,u.nation,u.borndata,u.born,u.home_tel,u.place_adress,u.place_zip_code
            ,u.adress,u.zip_code,u.email,u.education,u.political_status,u.exi_people,u.exi_tel
            ,u.relative,u.experience,u.condition,u.anamnesis,u.status,u.password,u.region_id,
            u.branch_id,u.position,u.entry_time,u.old_firm,u.need,u.account_number,u.friend,
            u.friend_relation,u.friend_name,u.friend_branch,u.friend_position,r.id as region_id,
            r.region,b.id as branch_id,b.branch')->join('oa_region re', 'u.region_id=re.id', 'left')
                ->join('oa_branch b', 'u.branch_id=b.id', 'left')
                ->join('oa_region r', 'u.region_id=r.id', 'left')
                ->where('u.id=' . $id)->find();
            //dump($res);die;
            //员工拥有的角色
            $role = Db::table('oa_user_role u,oa_role r')->field('r.id,u.state,r.role')->where(' u.role_id=r.id and u.user_id=' . $res['id'] .' and (r.state = 1 or r.state=2)')
                ->select();
            sort($role);
            //dump($role);die;
            $month = Db::table('oa_month')->select();
            $this->assign('role', $role);
            $this->assign('month', $month);
            $this->assign('list', $res);
            return $this->fetch();
        }
    }

    /**
     * @return int
     * 修改员工信息
     * @author  曹旺
     * @data  11:19
     */
    public function newstaff()
    {//修改员工信息

        $this->baseindex();
        if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {
            $res_start = input('post.data');
            $res = json_decode($res_start, true);
            $id = $res['id'];
            $arrs = $res['email'];
            $ch_name = $res['ch_name'];
            $en_name = $res['en_name'];
            $email = $this->getEmaila($arrs);
            if ($email) {
                //验证邮箱是否唯一
                $verity_email=$this->save_verify_email($res['id'],$res['email']);
                if (!$verity_email){
                    return 4;
                }
                //验证手机号唯一
                $verity_phone=$this->save_verify_phone($res['id'],$res['tel']);
                if (!$verity_phone){
                    return 5;
                }
                //验证中英文用户名不能为空
                if (!empty($ch_name) && !empty($en_name)) {
                    //验证中英文用户名不能重复
                    $result = Db::table('oa_user')->where(['ch_name' => $ch_name])
                        ->whereOr(['en_name' => $en_name])->select();
                    if (empty($result)) {
                        $aaa = Db::table('oa_user')->where('id=' . $id)->update($res);
                        //删除表里原始的数据
                        $clear = Db::table('oa_user_role')->where('user_id=' . $res['id'])->delete();
                        if ($clear) {
                            //初始化表里的数据
                            $role_a = Db::table('oa_role')->select();
                            foreach ($role_a as $kk => $vv) {
                                $data['user_id'] = $res['id'];
                                $data['role_id'] = $vv['id'];
                                Db::table('oa_user_role')->insert($data);
                            }
                            //取得所有字段的键
                            $key = array_keys($res);
                            for ($i = 0; $i < count($key); $i++) {
                                //排除键名不能转化成int型的键名
                                $key_s = intval($key[$i]);
                                if ($key_s != 0) {
                                    //添加数据
                                    if ($res[$key[$i]] == "on") {
                                        $date['user_id'] = $id;
                                        $date['role_id'] = $key[$i];
                                        Db::table('oa_user_role')->where($date)->update(['state' => 1]);
                                    }
                                }
                            }
                            return 1;
                        } else {
                            return 0;
                        }
                    } else {
                        if (count($result) == 1 && $result[0]['id'] == $id) {
                            Db::table('oa_user')->where('id=' . $id)->update($res);
                            //return 10;
                            $clear = Db::table('oa_user_role')->where('user_id=' . $res['id'])->delete();
                            if ($clear) {
                                $role_a = Db::table('oa_role')->select();
                                foreach ($role_a as $kk => $vv) {
                                    $data['user_id'] = $res['id'];
                                    $data['role_id'] = $vv['id'];
                                    Db::table('oa_user_role')->insert($data);
                                }
                                //取得所有字段的键
                                $key = array_keys($res);
                                for ($i = 0; $i < count($key); $i++) {
                                    //排除键名不能转化成int型的键名
                                    $key_s = intval($key[$i]);
                                    if ($key_s != 0) {
                                        if ($res[$key[$i]] == "on") {
                                            $date['user_id'] = $res['id'];
                                            $date['role_id'] = $key[$i];
                                            Db::table('oa_user_role')->where($date)->update(['state' => 1]);
                                        }
                                    }
                                }
                                return 1;
                            } else {
                                return 0;
                            }
                        } else {
                            return 0;
                        }
                    }
                } else {
                    return 3;
                }

            } else {
                return 2;
            }
        }
    }
}